<?php

// +----------------------------------------------------------------------
// | 简易CMS
// +----------------------------------------------------------------------
// | Copyright (C) 2010 Hoping Software Studio.
// +----------------------------------------------------------------------
// | Version $Id$
// +----------------------------------------------------------------------
namespace app\admin\model;

use CMS\Model;

/**
 * 插件模型
 * @author yangweijie <yangweijiester@gmail.com>
 */
class Menu extends Model {
    protected $_validate = array(
        array('title', 'require', '标题必须填写'),
        array('url', 'require', '链接必须填写'),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 删除某个菜单前先检查一下该菜单是否还有子菜单，如果还有子菜单则不允许删除
     * @param \CMS\Model $obj
     */
    protected static function before_delete($obj = array()) {
        $options = $obj->getAllOptions();       //因为后面要执行查询操作，所以这里得保存一下现在的options
        $ids = $obj->column('id');
        $child = $obj->where(array('pid' => array('IN', $ids)))->count();
        if ($child) {
            $obj->error = '指定的菜单还有子菜单未删除！';
            return false;
        }
        $obj->setOptions($options);         //恢复已经保存的options
        return true;
    }

}
